﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using MouseExtender.Logic.Entities;
using MouseExtender.Logic.Managers;

namespace MouseExtender.Controls.UserControls
{
	/// <summary>
	/// Interaction logic for RemoveTabUC.xaml
	/// </summary>
	public partial class RemoveTabUC : UserControl
	{
		public int RemoveTabId
		{
			get;
			private set;
		}

		public RemoveTabUC(int removeTabId)
		{
			InitializeComponent();
			RemoveTabId = removeTabId;
			PrepareTabList();
		}

		private void PrepareTabList()
		{
			List<TabInfo> tabInfoList = new List<TabInfo>();
			SettingsManager.CurrentSettings.TabInfoList.ForEach(tab =>
				{
					if (tab.Id != RemoveTabId) tabInfoList.Add(tab);
				}
			);
			if (tabInfoList.Count == 0)
			{
				tabInfoList.Add(
					new TabInfo()
					{
						Id = -1,
						Name = "Panel without tabs."
					}
				);
			}
			cbTabList.ItemsSource = tabInfoList;
		}

		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{
			rbRemoveAll.Focus();
		}

		private void RadioButton_Checked(object sender, RoutedEventArgs e)
		{
			cbTabList.IsEnabled = true;
			cbTabList.SelectedIndex = 0;
		}

		private void RadioButton_Unchecked(object sender, RoutedEventArgs e)
		{
			cbTabList.IsEnabled = false;
			cbTabList.SelectedIndex = -1;
		}

		private void OK_Click(object sender, RoutedEventArgs e)
		{
			MainWindow mainWindow = App.MainWindowInstance;
			
			if (rbRemoveAll.IsChecked.Value)
			{
				mainWindow.ucTabs.RemoveTab(RemoveTabId);
			}
			else
			{
				mainWindow.ucTabs.RemoveTab(RemoveTabId, ((TabInfo)cbTabList.SelectedItem).Id);
			}
			mainWindow.IsOpenPopup = false;
		}
	}
}
